import {
    reqShopList,
    reqShopDel,
    reqShopAdd
} from "../../request/api";
import { createSelector } from "reselect"
//初始值
const initState = {
    shopList: [],
    allFlag: false
}

//actionTypes
const types = {
    CHANGE_SHOP_LIST: "SHOP_CHANGE_SHOP_LIST",
    CHANGE_SHOP_LIST_ONE: "SHOP_CHANGE_SHOP_LIST_ONE",
    CHANGE_ALL_FLAG: "SHOP_CHANGE_ALL_FLAG",
}

//reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_SHOP_LIST:
            return {
                ...state,
                shopList: action.arr
            }
        case types.CHANGE_ALL_FLAG:
            state.shopList[1].content.forEach(item => {
                item.flag = action.flag
            })
            return {
                ...state,
                allFlag: action.flag,
                    shopList: [...state.shopList]
            }
        case types.CHANGE_SHOP_LIST_ONE:
            state.shopList[1].content[action.num].flag =action.flag
            state.allFlag = state.shopList[1].contentevery(item => item.flag)
            return {
                ...state,
                shopList: [...state.shopList]
            }
        default:
            return state;
    }
}

//actionCreators组件派发的动作
export const actions = {
    changeShopList: (arr) => ({
        type: types.CHANGE_SHOP_LIST,
        arr
    }),
    changeShopListOne: (flag, num) => ({
        type: types.CHANGE_SHOP_LIST_ONE,
        flag,
        num
    }),
    changeAllFlag: (flag) => ({
        type: types.CHANGE_ALL_FLAG,
        flag
    }),


    reqShopList: () => (dispatch, getState) => {
        reqShopList({
            uid: JSON.parse(sessionStorage.getItem("user")).uid
        }).then(res => {
            if (res.data.code === 200) {
                res.data.list[1].content.forEach(item => {
                    item.flag = false
                })
                dispatch(actions.changeShopList(res.data.list))
            }
        })
    },
    reqShopDel: (id) => (dispatch, getState) => {
        reqShopDel({
            id: id
        }).then(res => {
            if (res.data.code === 200) {
                dispatch(actions.reqShopList())
            }
        })
    },
    reqShopAdd: (id) => (dispatch, getState) => {
        reqShopAdd({
            uid: JSON.parse(sessionStorage.getItem("user")).uid,
            type: 1,
            goodsid: id,
            num: 1
        }).then(res => {
            if (res.data.code === 200) {
                dispatch(actions.reqShopList())
            }
        })
    },
}

//selectors导出数据
export const getShopList = (state) => state.shop.shopList
export const getAllFlag = (state) => state.shop.allFlag
export const getPrice = createSelector(
    [getShopList,getAllFlag],
    (shopList,allFlag)=>{
        if(shopList[1]){
            return shopList[1].content.reduce((he,item)=>{
                if(item.flag){
                    return he+item.num*item.price
                }else{
                    return he
                }
            },0)
        }
    }
)

export default reducer;